package com.md.youyouhui.ui.shopping;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityAddressMangerBinding;
import com.md.youyouhui.databinding.AdapterAddressMangerBinding;
import com.md.youyouhui.net.response.GetAddressResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.ui.home.AddressActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/24.
 */
public class AddressMangerActivity extends BaseActivity {
    private ActivityAddressMangerBinding mangerBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;
    private int size;
    private boolean check;

    @Override
    public void setContentLayout() {
        mangerBinding = DataBindingUtil.setContentView(this, R.layout.activity_address_manger);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(mangerBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        check = getIntent().getBooleanExtra("check", false);
        adapter = new MyAdapter();
        mangerBinding.mangerRecycler.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_address, menu);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    private void getAddressList() {
        viewModel.getAddress().observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    mangerBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    mangerBinding.progress.setVisibility(View.GONE);
                    List<GetAddressResponse> data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    size = data.size();
                    adapter.upDate(data);
                    break;
                case ERROR:
                    mangerBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.add) {
            startActivity(new Intent(this, AddressActivity.class).putExtra("size", size));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterAddressMangerBinding>> {
        List<GetAddressResponse> data;

        void upDate(List<GetAddressResponse> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterAddressMangerBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterAddressMangerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(ct), R.layout.adapter_address_manger, viewGroup, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterAddressMangerBinding> holder, int i) {
            holder.binding.mangerName.setText(data.get(i).consignee);
            holder.binding.mangerPhone.setText(data.get(i).mobilePhone);
            holder.binding.mangerAdd.setText(String.format("%s%s", data.get(i).area, data.get(i).detailedAddress));
            if ("0".equals(data.get(i).tag)) {
                holder.binding.mangerDefault.setVisibility(View.GONE);
            } else {
                holder.binding.mangerDefault.setVisibility(View.VISIBLE);
            }
            holder.itemView.setOnClickListener(v -> {
                if (check) {
                    //从地址管理进来，点击失效
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("address", data.get(i));
                Intent intent = new Intent();
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            });
            holder.itemView.setOnLongClickListener(v -> {
                mangerAddress(data.get(i));
                return true;
            });
        }

        @Override
        public int getItemCount() {
            return null == data ? 0 : data.size();
        }
    }

    public void mangerAddress(GetAddressResponse address) {
        new AlertDialog.Builder(ct).setMessage("编辑地址")
                .setNegativeButton("编辑", (dialog, which) -> {
                    dialog.dismiss();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("address", address);
                    Intent intent = new Intent(ct, AddressActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }).setPositiveButton("删除", (dialog, which) -> {
            dialog.dismiss();
            delAddress(address.id);
        }).show();
    }

    private void delAddress(String id) {
        viewModel.delAddress(id).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case SUCCESS:
                    getAddressList();
                    break;
                case ERROR:
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
